package com.syx.client.view;

import com.syx.client.service.FileClientService;
import com.syx.client.service.MessageClientService;
import com.syx.client.service.UserClientService;

import java.util.Scanner;

/**
 * @author 邵一鑫
 * @date 2024/1/13 20:40
 */
public class QQView {

    private boolean loop = true;    // 控制是否显示菜单

    String userId;  // 输入的用户名

    private Scanner scanner = new Scanner(System.in);

    private UserClientService userClientService = new UserClientService(); // 用户相关服务

    private MessageClientService messageClientService = new MessageClientService(); // 消息相关服务

    private FileClientService fileClientService = new FileClientService(); // 文件相关服务

    /**
     * 显示主菜单
     */
    public void mainMenu() {

        String key;     // 输入的指令
        String pwd;     // 输入的密码

        while (loop) {
            System.out.println("==========欢迎登录网络通信系统==========");
            System.out.println("1.登陆系统");
            System.out.println("2.退出系统");
            System.out.println("请输入你的选择：");

            key = scanner.next();

            switch (key) {
                case "1":
                    System.out.println("请输入用户名：");
                    userId = scanner.next();
                    System.out.println("请输入密码：");
                    pwd = scanner.next();
                    if (userClientService.checkUser(userId, pwd)) {
                        System.out.println("==========欢迎==========");
                        // 进入二级菜单
                        secondMenu();
                    } else {
                        System.out.println("账号或密码错误...");
                    }
                    break;
                case "2":
                    loop = false;
                    System.out.println("系统即将退出...");
                    break;
                default: {
                    System.out.println("请输入正确的编号~");
                }
            }

        }
    }

    /**
     * 显示二级菜单
     */
    public void secondMenu() {

        String key; // 输入的指令
        String toUser;  // 消息接收方
        String content; // 消息内容
        String fromPath;    // 文件发送方的路径
        String toPath;  // 文件接收方的路径

        while (loop) {
            System.out.println("==========网络通信系统二级菜单===========");
            System.out.println("1.显示在线用户列表");
            System.out.println("2.群发信息");
            System.out.println("3.私聊消息");
            System.out.println("4.发送文件");
            System.out.println("5.退出系统");
            System.out.println("请输入你的选择：");

            key = scanner.next();

            switch (key) {
                case "1":
                    userClientService.getOnlineFriendList();
                    break;
                case "2":
                    System.out.println("请输入要发送的消息内容：");
                    content = scanner.next();
                    messageClientService.sendMessageToAll(userId, content);
                    break;
                case "3":
                    System.out.println("请输入你想聊天的用户号（在线）：");
                    toUser = scanner.next();
                    System.out.println("请输入消息：");
                    content = scanner.next();
                    messageClientService.sendMessageToUser(userId, toUser, content);
                    break;
                case "4":
                    System.out.println("请输入接收文件的用户编号（在线）：");
                    toUser = scanner.next();
                    System.out.println("请输入待发送文件的完整路径：");
                    fromPath = scanner.next();
                    System.out.println("请输入接收方的完整路径：");
                    toPath = scanner.next();
                    fileClientService.sendFileToUser(userId, toUser, fromPath, toPath);
                    break;
                case "5":
                    userClientService.logout();
                    loop = false;
                    System.out.println("系统即将退出...");
                    break;
                default:
                    System.out.println("请输入正确的编号~");
            }
        }
    }


}
